package cn.com.godliu.gateway.aes;

import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
 * 生产摘要
 */
public class RequestSignUtil {

    private final static String key = "key";

    public static Map<String, String> genSignForGet(TreeMap<String, String> params) {
        Map<String, String> headMap = new HashMap<>();
        long timestamp = System.currentTimeMillis();
        StringBuilder paramValues = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (!StringUtils.equals("sign", entry.getKey())) {
                paramValues.append(entry.getKey()).append(entry.getValue());
            }

        }
        String md5 = MD5Util.digest(paramValues.toString() + key + timestamp);
        headMap.put("sign", md5);
        headMap.put("timestamp", String.valueOf(timestamp));
        return headMap;
    }

    public static Map<String, String> genSignForPost(String body) {
        Map<String, String> headMap = new HashMap<>();
        long timestamp = System.currentTimeMillis();

        String md5 = MD5Util.digest(body.toString() + key + timestamp);
        headMap.put("sign", md5);
        headMap.put("timestamp", String.valueOf(timestamp));
        return headMap;
    }

    public static void main(String[] args) {
        TreeMap<String, String> params = new TreeMap<>();
        params.put("type", "android");
        Map<String, String> headerMap = genSignForGet(params);
        System.out.println(headerMap);
        //String body="{\"playEndTime\":\"2021-06-24 16:11:59\",\"pageSize\":10,\"deviceNum\":\"3YSB034415WUU76\",\"playStartTime\":\"2021-06-24 16:01:49\",\"pageNo\":1,\"requestUrl\":\"/cloudvideo/getCloudVideos/\",\"account\":\"18106700207\"}";
        //headerMap=genSignForPost(body);
        //System.out.println(headerMap);
    }

}
